/*
 * Passenger.java
 *
 * Version:
 *  $Id: Passenger.java 2 2011-03-29 07:21:11Z jhaberstro $
 *
 *
 * Revisions:
 *  $Rev: 2 $
 *
 */

/**
 * Passenger representation class
 * @author jhaberstro
 */
public class Passenger implements Comparable< Passenger >
{
    /**
     * Passenger's constructor
     * @param name - the name of the passenger
     * @param group - the group of the passenger
     * @param sequenceNum - the sequence of the passenger
     */
    public Passenger(String name, String group, int sequenceNum) {
        name_ = name;
        group_ = group;
        sequenceNum_ = sequenceNum;
    }

    /**
     * Accessor method for the name
     * @return the name of the passenger
     */
    public String getName() {
        return name_;
    }

    /**
     * Accessor method for the group
     * @return the group of the passenger
     */
    public String getGroup() {
        return group_;
    }

    /**
     * Accessor for the sequence
     * @return the sequence of the passenger
     */
    public int getSequence() {
        return sequenceNum_;
    }

    /**
     * Compares a passenger's priority to another passenger
     * @param other - the other passenger to compare against
     * @return -1 if a lesser priority, 0 if the same priority, and 1 if a
     * higher priority
     */
    public int compareTo(Passenger other) {
        // The lower the priority value,
        // the higher the priority it is.
        // Thus 0 > 1.
        int v1 = getPriority();
        int v2 = other.getPriority();
        if (v1 > v2) {
            return -1;
        }
        else if (v1 < v2) {
            return 1;
        }
        
        return 0;
    } 

    /**
     * Gets the passenger's priority
     * @return an integer value representing the priority
     */
    private int getPriority() {
        int groupValue = -1;
        if (group_.equals("A")) {
            groupValue = 0;
        }
        else if (group_.equals("B")) {
            groupValue = 1;
        }
        else if (group_.equals("C")) {
            groupValue = 2;
        }
        
        assert(groupValue >= 0);
        
        return (groupValue << 8) | (sequenceNum_ & 0xff);
    }

    /**
     * main method - used here as a test program
     * @param args - unused
     */
    public static void main(String args[]) {        
        // Test 1
        {
            Passenger p = new Passenger("Bob", "A", 1);
            assert (p.getName().equals("Bob"));
            assert (p.getGroup().equals("A"));
            assert (p.getSequence() == 1);
        }
        
        // Test 2
        {
            Passenger p1 = new Passenger("p1", "A", 1);
            Passenger p2 = new Passenger("p2", "A", 2);
            assert (p1.compareTo(p2) > 0);
        }
        
        // Test 3
        {
            Passenger p1 = new Passenger("p1", "A", 60);
            Passenger p2 = new Passenger("p2", "B", 1);
            assert (p1.compareTo(p2) > 0);
            assert (p2.compareTo(p1) < 0);
        }
        
        // Test 4
        {
            Passenger p1 = new Passenger("p1", "B", 60);
            Passenger p2 = new Passenger("p2", "C", 1);
            assert (p1.compareTo(p2) > 0);
            assert (p2.compareTo(p1) < 0);
        }
        
        // Test 5
        {
            Passenger p1 = new Passenger("p1", "B", 30);
            Passenger p2 = new Passenger("p2", "B", 30);
            assert (p1.compareTo(p2) == 0) : "Whoops";
        }
    }


    /**
     * the name field
     */
    private String name_;

    /**
     * the group field
     */
    private String group_;

    /**
     * the sequence number field
     */
    private int sequenceNum_;
}